package com.scalar.chiptrack.adaptor.chartered;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.MutableAttributeSet;
import java.io.Reader;
import java.io.FileReader;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: ChandraSekhar
 * Date: Aug 30, 2006
 * Time: 4:40:04 AM
 */
public class CharteredHtmlWipParser extends HTMLEditorKit.ParserCallback
{
    private String m_Number = null;
    private CharteredWipStatus wipStatus = null;
    private ArrayList m_fabList = new ArrayList();
    private int m_tableTagCount; //keeps the count of number of table tags encountered
    private boolean m_readyForParsing;  //if set to true indicates that the parser has reached the data to be parsed
    private int m_cellIndex;
    private boolean m_cellOpen;
    private boolean m_invalidRow;
    private String m_reqestFrom;

    private String m_packageType = ""; //stores the package type
    private String m_partNumber = "";  //stores the part number
    private String m_poNumber = "";    //stores the po number



    public CharteredHtmlWipParser()
    {
         m_tableTagCount = 0;
         m_readyForParsing = false;
         m_cellOpen = false;
         m_cellIndex = -1;
         m_invalidRow = false;
    }

    public void parse(Reader reader, String adaptor) throws Exception
    {
        m_reqestFrom = adaptor;

        DTD dtd = DTD.getDTD("HTML");
        DocumentParser documentParser = new DocumentParser (dtd);
        documentParser.parse (reader, this, true /*ignore CharSet*/);
    }

    public ArrayList parse( FileReader reader ) throws Exception
    {
        DTD dtd = DTD.getDTD( "HTML" );
        DocumentParser documentParser = new DocumentParser( dtd );
        documentParser.parse( reader, this, true /*ignore CharSet*/ );

        for ( int i = 0; i < m_fabList.size(); i++ )
        {
            wipStatus = ( CharteredWipStatus ) m_fabList.get( i );

        }
        return m_fabList;
    }

    public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
    {
        // For getting the Latest for ADAPTOR
        
        if(t == HTML.Tag.INPUT)
        {
            HTML.Attribute value = HTML.getAttributeKey("value");

            if (  a.getAttribute(value).toString() != null && a.getAttribute(value).toString().indexOf("p003dtd_wip") != -1 )
            {
                if ( m_Number == null)
                {
                    setNumber( a.getAttribute(value).toString().substring(0,7) );
                }
            }

            if ( a.getAttribute(value).toString() != null && ( a.getAttribute(value).toString().indexOf("s002dtd_shipment_alert") != -1 || a.getAttribute(value).toString().indexOf("s002mtd_shipment_alert") != -1 ) )
            {
                if ( m_Number == null)
                {
                    setNumber( a.getAttribute(value).toString().substring(0,7) );
                }
            }
        }
        if (m_reqestFrom != null && m_Number == null )
        {
            m_readyForParsing = true;
            wipStatus = new CharteredWipStatus();
        }

        // FOR PARSING THE DOWNLOADED WIP STATUS FILE
        if ( t == HTML.Tag.TABLE )
        {
            m_tableTagCount++;

            //data is present in third table tag. So setting the variable to true to indicate the start of data
            if ( m_tableTagCount == 15 )
            {
                m_readyForParsing = true;

            }
            else if ( m_tableTagCount == 16 )
            {
                //this block is entered when the table containing data is parsed
                if ( ( wipStatus != null ) && ( m_invalidRow == false ) )
                {
                    m_fabList.add( wipStatus );
                }

                m_readyForParsing = false;
            }
        }

        if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TR ) && ( a.getAttributeCount() == 0 ) )
            {

                if ( ( wipStatus != null ) )
                {
                    m_fabList.add( wipStatus );
                }

                wipStatus = new CharteredWipStatus();
                m_cellIndex = -1;
                m_invalidRow = false;
            }
        }

        // Got a new cell
        if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TD ) )
            {
                if ( m_cellOpen == false )
                {
                    m_cellIndex++;
                    m_cellOpen = true;
                }
                else
                {
                    m_cellOpen = false;
                }
            }
        }
    }

    public void handleText( char[] data, int pos )
    {
        if ( ( m_readyForParsing == false ) || ( wipStatus == null ) )
        {
            return;
        }

        String text = new String( data );

        if ( text == null )
        {
            text = "";
        }
        else
        {
            text = text.trim();

        }
        //System.out.println("text = " + text);
        if ("There are no documents in this folder that you have access to.".equalsIgnoreCase( text ) )
        {
            setNumber("1");
        }
        switch ( m_cellIndex )
        {

            case 0:

                if ( !( "".equals( text ) ) && !( "Fab Group".equals( text.trim() ) ) )
                {
                    wipStatus.setFabGroup(text);
                }
                break;

            case 1:
                if ( !( "".equals( text ) ) && !( "Chartered Part Name".equals( text ) ) )
                {
                    wipStatus.setVendorPartNumber( text );
                }
                break;

            case 2:

                if ( !( "".equals( text ) ) && !( "Customer Part Name".equalsIgnoreCase( text ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setCustomerProduct( text );
                }

                break;

            case 3:
                if ( !( "".equals( text ) ) && !( "Geometry Name".equalsIgnoreCase( text.trim() ) )  &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setGeometryname( text );
                }
                break;

            case 4:
                if ( !( "".equals( text ) ) && !( "Process Name".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") )  )
                {
                    wipStatus.setProcessName( text );
                }
                break;

            case 5:
                if ( !( "".equals( text ) ) && !( "PO#".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
                {
                    wipStatus.setCustPo( text );
                }
                break;

            case 6:
               if ( !( "".equals( text ) ) && !( "SO#".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
               {
                   wipStatus.setSoNo( text );
               }
               break;

            case 7:
                if ( !( "".equals( text ) ) && !( "Lot Id".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setCustomerLot( text );
                }
                break;

            case 8:
                if ( !( "".equals( text ) ) && !( "Lot Type".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setLotType( text );
                }
                break;

            case 9:
                if ( !( "".equals( text ) ) && !( "Lot Priority".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setLotCategory( text );
                }
                break;

            case 10:
                if ( !( "".equals( text ) ) && !( "Location".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setLocation( text );
                }
                break;

            case 11:
                if ( !( "".equals( text ) ) && !( "Lot State".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setLotState( text );
                }
                break;

            case 12:
                if ( !( "".equals( text ) ) && !( "Stage Name".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setCurrentStage( text );
                }
                break;

            case 13:
                if ( !( "".equals( text ) ) && !( "Curr Stage #".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setStagePosition( text );
                }
                break;

            case 14:
                if ( !( "".equals( text ) ) && !( "Curr Step #".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setCurrStepNo( text );
                }
                break;

            case 15:
                if ( !( "".equals( text ) ) && !( "Balance Mask #".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setBalanceMaskNo( text );
                }
                break;

            case 16:
                if ( !( "".equals( text ) ) && !( "Lot Wfr Qty".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setWaferQty( text );
                }
                break;

            case 17:
                if ( !( "".equals( text ) ) && !( "Wafer ID".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setWaferID( text );
                }
                break;

            case 18:
                if ( !( "".equals( text ) ) && !( "Actual Start Date".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setDateIn( text );
                }
                break;

            case 19:
                if ( !( "".equals( text ) ) && !( "Act Fabout Date".equalsIgnoreCase( text.trim() ) ) && !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setActFaboutDate( text );
                }
                break;

            case 20:
                if ( !( "".equals( text ) ) && !( "CRD".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setExpectedDateOut( text );
                }
                break;

            case 21:
                if ( !( "".equals( text ) ) && !( "OSD".equalsIgnoreCase( text.trim() ) )  &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setOsdDate( text );
                }
                break;

            case 22:
                if ( !( "".equals( text ) ) && !( "CSD".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setCommitDate( text );
                }
                break;

            case 23:
                if ( !( "".equals( text ) ) && !( "EDO".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setEdoDate( text );
                }
                break;
           default:
                break;
        }

    }

    // Resets the objects state
    public void reset()
    {
        m_tableTagCount = 0;
        m_readyForParsing = false;
        m_cellOpen = false;
        m_cellIndex = -1;
        m_invalidRow = false;
    }

    public String getNumber() {
        return m_Number;
    }

    public void setNumber(String m_Number) {
        this.m_Number = m_Number;
    }
}
